import { _decorator, AnimationClip,Animation, SkeletalAnimation } from 'cc';
import { EntityTypeEnum } from '../../Common';
import StateMachine,{getInitParamsTrigger} from '../../Base/StateMachine';
import { EntityStateEnum, ParamsNameEnum } from '../../Enum';
import State from '../../Base/State';
import { ObjectPoolManager } from '../../Global/ObjectPoolManager';
const { ccclass, property } = _decorator;

@ccclass('ExplosionStateMachine')
export class ExplosionStateMachine extends StateMachine {
    init(type: EntityTypeEnum) {
        this.type = type;
        this.animationComponent = this.node.addComponent(Animation);
        this.initParams();
        this.initStateMachines();
        this.initAnimationEvent();
      }
      
      /**
       * 初始化状态机参数，添加状态机参数
       */
      initParams() {
        this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger());
      }

      /**
       * 初始化状态机,new State的构造方法需要传入当前状态机对象，动画路径和动画循环方式
       */
      initStateMachines() {
        this.stateMachines.set(ParamsNameEnum.Idle, 
            new State(this, `${this.type}${EntityStateEnum.Idle}`,AnimationClip.WrapMode.Normal)
        );
      }
      
      /**
       * 初始化动画事件，监听动画播放完毕事件，播放完毕后销毁节点
       * 在cocos文档的动画事件中可以找到代码
       * */
      initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED,()=>{
            // this.node.destroy()
            ObjectPoolManager.Instance.ret(this.node)
            
        //动画结束销毁组件
            this.node.getComponent(Animation).destroy()
        })
      }
      
      /**
       * 两个状态之间的条件判断，根据当前状态机params的值来决定currentState当前状态机状态
       */
      run() {
        switch (this.currentState) {
          case this.stateMachines.get(ParamsNameEnum.Idle):
            if (this.params.get(ParamsNameEnum.Idle).value) {
              this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
            } else {
              this.currentState = this.currentState;
            }
            break;
          default:
            this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
            break;
        }
      }
}

